package com.spring.Repository;

import com.spring.Entity.ContractEntity;
import org.hibernate.SQLQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Arabira on 2016/8/3.
 */
@Repository
public interface ContractRepository extends JpaRepository<ContractEntity, String> {
    @Query("from ContractEntity c where c.userId = ?1 ")
    Page<Object> findByUserIdPageable(String userId, Pageable pageable);

    @Query("from ContractEntity c where c.remind = ?1 and c.warnOrNor = true ")
    ContractEntity[] findByRemindTimeAndWarnOrNot(LocalDate now);

    //    @Query("from ContractEntity c ")
//    Page<Object> findAllPageable(Pageable pag);
//    @Query("from ContractEntity c join UserEntity u where c.userid=u.id")
//@Query("select * from contract")
//    ContractEntity[] findAllPageable();
    @Query("from ContractEntity c, UserEntity u where c.userId=u.id order by c.deadline")
    List findAllPageable();

    @Query("from ContractEntity c, UserEntity u where c.userId=u.id and c.contractId like ? and c.contractName like ? and u.userName like ? and c.start between ? and ? and c.deadline between ? and ?  order by c.deadline")
    List findReqByAdmin(String contractId, String contractName, String userName, Date startS, Date startE, Date deadlineS, Date deadlineE);

    @Query("from ContractEntity c where c.contractId = ?1 ")
    ContractEntity findByContractID(String contractId);

    @Query("from ContractEntity c where c.userId = ?1")
    List findAllByUserId(String userId);
}
